package com.distribution.system.manager.channel.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.IdUtil;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.distribution.common.utils.StringUtils;
import com.distribution.system.domain.bo.*;
import com.distribution.system.domain.dto.ChoosePhoneDTO;
import com.distribution.system.domain.dto.SendCodeDTO;
import com.distribution.system.domain.entity.AddressChannel;
import com.distribution.system.domain.entity.CmccDaOrder;
import com.distribution.system.domain.entity.CmccDaProduct;
import com.distribution.system.domain.entity.CmccDaUpChannelInfo;
import com.distribution.system.domain.req.ReqResult;
import com.distribution.system.manager.channel.AbstractChannel;
import com.distribution.system.manager.channel.channelInfo.enums.MaiZhongChannelEnum;
import com.distribution.system.service.AddressChannelService;
import com.distribution.system.utils.http.HttpRequestUtils;
import lombok.extern.slf4j.Slf4j;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Liping Huo
 * @date 2023/5/23 1:05
 */
@Slf4j
public class MaiZhongChannel extends AbstractChannel {

    public MaiZhongChannel(CmccDaUpChannelInfoBO cmccDaUpChannelInfoBO) {
        super(cmccDaUpChannelInfoBO);
    }

    @Override
    public AddressInfoBo getChannelAddressInfo(AddressNameBo addressNameBo, AddressChannelService addressChannelService) {
        AddressInfoBo addressInfoBo = new AddressInfoBo();
        HashMap<String, Object> map = new HashMap<>(1 << 4);
        //配置请求参数
        map.put("channelCode", interfaceParamsMap.get(MaiZhongChannelEnum.CHANNEL_CODE.getName()));// 渠道编码
//        System.out.println(interfaceUrlsMap.get(MaiZhongChannelEnum.GET_AREA_CODE.getName()));
        String responseJsonStr = HttpRequestUtils.sendPost(interfaceUrlsMap.get(MaiZhongChannelEnum.GET_AREA_CODE.getName()), new JSONObject(map).toString());
        JSONObject responseJson = JSONObject.parseObject(responseJsonStr);
        JSONArray dataArray = responseJson.getJSONArray("data");
        for (Object provinceObj : dataArray) {
            JSONObject provinceJson = (JSONObject) provinceObj;
            String provinceName = provinceJson.getString("name");
            String provinceCode = provinceJson.getString("code");
            if (StringUtils.equals(addressNameBo.getAttributeProvince().substring(0,2), provinceName.substring(0,2))){
                addressInfoBo.getAttributeInfo().setProvinceName(provinceName);
                addressInfoBo.getAttributeInfo().setProvinceCode(provinceCode);
            }
            if (StringUtils.equals(addressNameBo.getAddressProvince().substring(0,2), provinceName.substring(0,2))){
                addressInfoBo.getAddressInfo().setProvinceName(provinceName);
                addressInfoBo.getAddressInfo().setProvinceCode(provinceCode);
            }
            JSONArray cityArray = provinceJson.getJSONArray("subList");
            for (Object cityObj : cityArray) {
                JSONObject cityJson = (JSONObject) cityObj;
                String cityName = cityJson.getString("name");
                String cityCode = cityJson.getString("code");
                if (StringUtils.equals(addressNameBo.getAttributeCity().substring(0,2), cityName.substring(0,2))){
                    addressInfoBo.getAttributeInfo().setCityName(cityName);
                    addressInfoBo.getAttributeInfo().setCityCode(cityCode);
                }
                if (StringUtils.equals(addressNameBo.getAddressCity().substring(0,2), cityName.substring(0,2))){
                    addressInfoBo.getAddressInfo().setCityName(cityName);
                    addressInfoBo.getAddressInfo().setCityCode(cityCode);
                }
                JSONArray districtArray = cityJson.getJSONArray("subList");
                for (Object districtObj : districtArray) {
                    JSONObject districtJson = (JSONObject) districtObj;
                    String districtName = districtJson.getString("name");
                    String districtCode = districtJson.getString("code");
                    if (StringUtils.equals(addressNameBo.getAddressDistrict().substring(0,2), districtName.substring(0,2))){
                        addressInfoBo.getAddressInfo().setDistrictName(districtName);
                        addressInfoBo.getAddressInfo().setDistrictCode(districtCode);
                    }
                }
            }
        }
        return addressInfoBo;
    }

    @Override
    public String configPlaceOrderRequestParams(CmccDaOrder cmccDaOrder, CmccDaProduct cmccDaProduct, AddressInfoBo addressInfoBo) {
        log.info("配置麦众下单接口参数...");
        AddressChannel addressInfo = addressInfoBo.getAddressInfo();
        // 生成订单号
        orderId = "MZ" + DateUtil.format(DateUtil.date(), "yyyyMMdd") + IdUtil.getSnowflake(1, 1).nextIdStr().substring(8,18);
        HashMap<String, Object> map = new HashMap<>(1 << 4);
        //配置请求参数
        map.put("upOrderCode", orderId);// 你方订单号
        map.put("channelCode", interfaceParamsMap.get(MaiZhongChannelEnum.CHANNEL_CODE.getName()));// 渠道编码
        map.put("goodsId", cmccDaOrder.getProductParam());// 商品id
        map.put("concatName", cmccDaOrder.getCustomerName());// 收货人
        map.put("concatNum", cmccDaOrder.getCustomerCertNo());// 身份证号码
        map.put("concatTel", cmccDaOrder.getCustomerPhone());// 联系电话
        map.put("postProvinceCode", addressInfo.getProvinceCode());// 省级编码 归属地编码
        map.put("postProvinceName", addressInfo.getProvinceName());// 省名称
        map.put("postCityCode", addressInfo.getCityCode());// 市级编码 归属地编码
        map.put("postCityName", addressInfo.getCityName());// 市名称
        map.put("postDistrictCode", addressInfo.getDistrictCode());// 区级编码 邮寄地编码
        map.put("postDistrictName", addressInfo.getDistrictName());// 区名称
        map.put("postAddress", addressInfo.getProvinceName() + addressInfo.getCityName() + addressInfo.getDistrictName() + cmccDaOrder.getAddressStreet());// 详细地址
        //map.put("pageLink", "");// 下单链接（客户下单的h5地址）非必传
        map.put("callbackUrl", interfaceParamsMap.get(MaiZhongChannelEnum.CALLBACK_URL.getName()));// 订单状态同步 回调地址
        //map.put("verifyCode", "");// 验证码（部分渠道非必传）
        return new JSONObject(map).toString();
    }

    @Override
    public String sendPlaceOrderRequest(String requestJsonStr) {
        return HttpRequestUtils.sendPost(interfaceUrlsMap.get(MaiZhongChannelEnum.PLACE_ORDER.getName()), requestJsonStr);
    }

    @Override
    public ReqResult parsePlaceOrderResponseStr(JSONObject responseJsonObject) {
        //响应
        String message = responseJsonObject.getString("message");
        if (responseJsonObject.getInteger("status") != 200) {// 失败
            return ReqResult.error(message);
        } else {
            JSONObject data = responseJsonObject.getJSONObject("data");
            UnOrderResultBo unOrderResultBo = new UnOrderResultBo();
            unOrderResultBo.setOrderId(data.getString("upOrderCode"));
            unOrderResultBo.setChannelOrderNo(data.getString("orderCode"));
            //unOrderResultBo.setChoosePhoneNum(data.getString("number"));
            return ReqResult.success(message, unOrderResultBo);
        }
    }

    @Override
    public String configChoosePhoneRequestParams(ChoosePhoneDTO choosePhoneDTO, CmccDaProduct cmccDaProduct, AddressInfoBo addressInfoBo) {
        return null;
    }

    @Override
    public String sendChoosePhoneRequest(String requestJsonStr) {
        return null;
    }

    @Override
    public ReqResult parseChoosePhoneResponseStr(JSONObject responseJsonObject) {
        return null;
    }

    @Override
    public ReqResult sendCardSmsCode(CmccDaProduct cmccDaProduct, SendCodeDTO sendCodeDTO) {
        return null;
    }


    @Override
    public RefreshOrderBO getRefreshOrderBO(CmccDaOrder cmccDaOrder) {
        return null;
    }

    @Override
    public Map<CmccDaOrder,RefreshOrderBO> getRefreshOrderBOMap(List<CmccDaOrder> orderList) {
        return null;
    }

    @Override
    public void refreshChannelCommission(CmccDaUpChannelInfo cmccDaUpChannelInfo) {
    }
}
